package com.tnt.webpageauthorization.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.itit.itf.okhttp.FastHttpClient;
import io.itit.itf.okhttp.Response;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 网页授权获取用的openid或基本信息
 * @Author zhuangxl
 * @Date 2021-01-15 15:03
 */
public class WechatUtil {


    private static final String APPID = "wxed4fb54af80d9c46";
    private static final String APPSECRET = "3a6902bbc276069019831f78b21ac280";

    /**
     * 微信网页授权地址及参数
     * https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
     * &response_type=code&scope=snsapi_base&state=123#wechat_redirect
     * &response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
     **/
    private static final String WEB_PAGE_AUTHORIZATION_BASE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize";
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
    private static final String USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo";



    /**
     * @Description: 获取网页授权URL
     * @Author: zhuangxl 
     * @Date: 21/1/15 20:23
     * @return: java.lang.String
     **/
    public static String getWebPageAuthorizationUrl(String redirect_uri, String scope, String state) throws Exception {

        String url = WechatUtil.WEB_PAGE_AUTHORIZATION_BASE_URL
                + "?appid=" + WechatUtil.APPID
                + "&redirect_uri=" + URLEncoder.encode(redirect_uri, "UTF-8")
                + "&response_type=code"
                + "&scope=" + scope
                + "&state=" + state
                + "#wechat_redirect";

        return url;
    }


    /**
     * @Description: 获取 openid
     * @Author: zhuangxl
     * @Date: 21/1/15 20:23
     * @return: java.lang.String
     **/
    public static String getOpenid(String code) throws Exception{

        String url = WechatUtil.ACCESS_TOKEN_URL
                + "?appid="+ WechatUtil.APPID
                + "&secret=" + WechatUtil.APPSECRET
                + "&code=" + code
                + "&grant_type=authorization_code";

        Response response = FastHttpClient.get().url(url).build().execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());

        return jsonObject.get("openid").toString();

    }

    /**
     * @Description: 获取 access_token 和 openid
     * @Author: zhuangxl
     * @Date: 21/1/15 20:23
     * @return: java.lang.String
     **/
    public static WxWebPageAccessToken getAccessTokenAndOpenid(String code) throws Exception{

        String url = WechatUtil.ACCESS_TOKEN_URL
                + "?appid="+ WechatUtil.APPID
                + "&secret=" + WechatUtil.APPSECRET
                + "&code=" + code
                + "&grant_type=authorization_code";

        Response response = FastHttpClient.get().url(url).build().execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        WxWebPageAccessToken wxWebPageAccessToken = JSON.toJavaObject(jsonObject, WxWebPageAccessToken.class);

        return wxWebPageAccessToken;

    }

    /**
     * @Description: 获取用户信息
     * @Author: zhuangxl
     * @Date: 21/1/15 20:23
     * @return: java.lang.String
     **/
    public static WxWebPageUserinfo getUserinfo(String access_token, String openid) throws Exception{

        String url = WechatUtil.USERINFO_URL
                + "?access_token=" + access_token
                + "&openid=" + openid
                + "&lang=zh_CN";

        Response response = FastHttpClient.get().url(url).build().execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        WxWebPageUserinfo userinfo = JSON.toJavaObject(jsonObject, WxWebPageUserinfo.class);

        return userinfo;

    }
}
